{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center>\n",
    "    <p style=\"text-align:center\">\n",
    "        <img alt=\"phoenix logo\" src=\"https://raw.githubusercontent.com/Arize-ai/phoenix-assets/9e6101d95936f4bd4d390efc9ce646dc6937fb2d/images/socal/github-large-banner-phoenix.jpg\" width=\"1000\"/>\n",
    "        <br>\n",
    "        <br>\n",
    "        <a href=\"https://arize.com/docs/phoenix/\">Docs</a>\n",
    "        |\n",
    "        <a href=\"https://github.com/Arize-ai/phoenix\">GitHub</a>\n",
    "        |\n",
    "        <a href=\"https://arize-ai.slack.com/join/shared_invite/zt-2w57bhem8-hq24MB6u7yE_ZF_ilOYSBw#/shared-invite/email\">Community</a>\n",
    "    </p>\n",
    "</center>\n",
    "<h1 align=\"center\">Tracing an Agno Policy Research Agent</h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -q agno duckduckgo-search newspaper4k lxml_html_clean openinference-instrumentation-agno openinference-instrumentation-openai openai arize-phoenix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to Phoenix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For this example you'll use a hosted version of Phoenix - if you don't already have an account, you can create one for free [here](https://app.phoenix.arize.com/). If you'd prefer to self-host Phoenix, then follow the [instructions here](https://arize.com/docs/phoenix/self-hosting)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from getpass import getpass\n",
    "\n",
    "if \"OPENAI_API_KEY\" not in os.environ:\n",
    "    os.environ[\"OPENAI_API_KEY\"] = getpass(\"🔑 Enter your OpenAI API key: \")\n",
    "\n",
    "if \"PHOENIX_API_KEY\" not in os.environ:\n",
    "    os.environ[\"PHOENIX_API_KEY\"] = getpass(\"🔑 Enter your Phoenix API key: \")\n",
    "\n",
    "if \"PHOENIX_COLLECTOR_ENDPOINT\" not in os.environ:\n",
    "    os.environ[\"PHOENIX_COLLECTOR_ENDPOINT\"] = getpass(\"🔑 Enter your Phoenix Collector Endpoint\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openinference.instrumentation.agno import AgnoInstrumentor\n",
    "from openinference.instrumentation.openai import OpenAIInstrumentor\n",
    "\n",
    "from phoenix.otel import register\n",
    "\n",
    "tracer_provider = register(project_name=\"agno\")\n",
    "AgnoInstrumentor().instrument(tracer_provider=tracer_provider)\n",
    "OpenAIInstrumentor().instrument(tracer_provider=tracer_provider)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Building your Agno Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from textwrap import dedent\n",
    "\n",
    "from agno.agent import Agent\n",
    "from agno.models.openai import OpenAIChat\n",
    "from agno.tools.duckduckgo import DuckDuckGoTools\n",
    "from agno.tools.newspaper4k import Newspaper4kTools\n",
    "\n",
    "policy_research_agent = Agent(\n",
    "    model=OpenAIChat(id=\"gpt-4o\"),\n",
    "    tools=[DuckDuckGoTools(), Newspaper4kTools()],\n",
    "    description=dedent(\"\"\"\\\n",
    "        You are a senior policy analyst who advises G20 governments on the\n",
    "        governance of advanced AI systems. Your expertise spans: 🌐\n",
    "\n",
    "        • Comparative regulatory analysis (EU AI Act, U.S. EO, China’s draft regs)\n",
    "        • Risk taxonomy & mitigation frameworks (NIST, ISO, OECD)\n",
    "        • Multi‑stakeholder negotiation and diplomacy\n",
    "        • Economic impact modelling and labour‑market studies\n",
    "        • Standards‑setting processes (IEEE, ISO/IEC)\n",
    "        • Enforcement mechanisms and audit requirements\n",
    "        • Rights‑based and ethics‑based approaches to AI governance\\\n",
    "    \"\"\"),\n",
    "    instructions=dedent(\"\"\"\\\n",
    "        1. Discovery Phase 🔍\n",
    "           – Gather at least 12 authoritative sources: legislation, white‑papers,\n",
    "             peer‑reviewed studies, think‑tank reports, and reputable news.\n",
    "           – Prioritise the most recent versions / amendments (≤ 12 months).\n",
    "           – Identify divergent regional approaches and key stakeholders.\n",
    "\n",
    "        2. Comparative Analysis 📊\n",
    "           – Map each region’s regulatory scope, risk tiers, and enforcement powers.\n",
    "           – Cross‑reference impact assessments and economic forecasts.\n",
    "           – Highlight areas of convergence and friction (e.g., foundation‑model audits).\n",
    "\n",
    "        3. Recommendation Draft ✍️\n",
    "           – Craft a concise, actionable brief for policymakers.\n",
    "           – Include trade‑offs, implementation timelines, and anticipated market effects.\n",
    "           – Use bullet points and tables where clarity improves.\n",
    "\n",
    "        4. Validation & Quality Control ✓\n",
    "           – Verify every cited statute / article for publication date and authenticity.\n",
    "           – Ensure balanced representation of civil‑society and industry viewpoints.\n",
    "           – Flag any major uncertainties or data gaps.\n",
    "    \"\"\"),\n",
    "    expected_output=dedent(\"\"\"\\\n",
    "        # {Short, Punchy Headline on AI Governance Landscape} 🌐\n",
    "\n",
    "        ## Executive Summary\n",
    "        {One‑paragraph snapshot of regulatory momentum and stakes}\n",
    "\n",
    "        | Region | Current Status | Key Provisions | Enforcement Timeline |\n",
    "        |--------|---------------|----------------|----------------------|\n",
    "        | EU     | ...           | ...            | ...                 |\n",
    "        | U.S.   | ...           | ...            | ...                 |\n",
    "        | ...    | ...           | ...            | ...                 |\n",
    "\n",
    "        ## Comparative Findings\n",
    "        - **Risk Classification:** {...}\n",
    "        - **Testing & Audit Requirements:** {...}\n",
    "        - **Penalties & Incentives:** {...}\n",
    "\n",
    "        ## Strategic Implications\n",
    "        {Market, innovation, and compliance impacts for enterprises}\n",
    "\n",
    "        ## Policy Recommendations\n",
    "        1. **Short‑Term (0‑12 mo):** {...}\n",
    "        2. **Medium‑Term (1‑3 yrs):** {...}\n",
    "        3. **Long‑Term (>3 yrs):** {...}\n",
    "\n",
    "        ## Sources\n",
    "        {Numbered list, each with publication date and 1‑line relevance note}\n",
    "\n",
    "        ---\n",
    "        Prepared by AI Policy Analyst · Published: {current_date} · Last Updated: {current_time}\n",
    "    \"\"\"),\n",
    "    markdown=True,\n",
    "    show_tool_calls=True,\n",
    "    add_datetime_to_instructions=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "policy_research_agent.print_response(\n",
    "    \"Analyze the current state and future implications of artificial intelligence regulation worldwide\",\n",
    "    stream=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## View your Traces in Phoenix!\n",
    "\n",
    "![traces in phoenix](https://storage.googleapis.com/arize-phoenix-assets/assets/images/agno-example-trace.png)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
